package com.example.service.impl;

import com.example.context.BaseContext;
import com.example.dto.BookDishDTO;
import com.example.mapper.BookDishMapper;
import com.example.result.PageBeanResult;
import com.example.service.BookDishService;
import com.example.vo.BookDishVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class BookDishImpl  implements BookDishService {
    @Autowired
    BookDishMapper bookDishMapper;

    /**
     * 分页展示所有图书
     *
     * @param name
     * @param categoryId
     * @param status
     * @param page
     * @param pageSize
     */
    @Override
    public PageBeanResult showBook(String name, Integer categoryId, Integer status, Integer page, Integer pageSize) {

        PageHelper.startPage(page,pageSize);
        Page<BookDishVO> p=bookDishMapper.showBook(name,categoryId,status,page,pageSize);
        long total = p.getTotal();
        List<BookDishVO> result = p.getResult();
        return new PageBeanResult(total,result);
    }

    /**
     * 根据名字查询图书
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBeanResult selectBook(String name, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        Page<BookDishDTO> p=bookDishMapper.selectBook(name,page,pageSize);
        long total = p.getTotal();
        List<BookDishDTO> result = p.getResult();
        return new PageBeanResult(total,result);
    }


    /**
     * 删除图书
     * @param id
     */
    @Override
    public void deleteBook(Integer id) {

        bookDishMapper.deleteBook(id);
    }


    /**
     * 批量删除图书
     * @param ids
     */
    @Override
    public void deleteBooks(List<String> ids) {

        bookDishMapper.deleteBooks(ids);

    }

    /**
     * 是否售完 1为未售完 0为售完
     * @param status
     * @param id
     */
    @Override
    public void starOrStop(Integer status, Integer id) {

        bookDishMapper.starOrStop(status,id);
    }

    /**
     * 数据回显
     *
     * @param id
     * @return
     */
    @Override
    public BookDishVO getById(Long id) {

        return bookDishMapper.getById(id);
    }


    /**
     * 修改图书信息
     * @param bookDishDTO
     */
    @Override
    public void updateBook(BookDishDTO bookDishDTO) {

        bookDishMapper.updateBook(bookDishDTO);

    }


    /**
     * 新增图书
     * @param bookDishDTO
     */
    @Override
    public void addBook(BookDishDTO bookDishDTO) {

        bookDishDTO.setCreateUser(BaseContext.getCurrentId());
        bookDishDTO.setUpdateUser(BaseContext.getCurrentId());
        bookDishMapper.addBook(bookDishDTO);

    }

    /**
     * 根据分类id查询图书
     * @param categoryId
     * @return
     */
    @Override
    public List<BookDishDTO> list(Long categoryId) {
        BookDishDTO bookDishDTO = BookDishDTO.builder()
                .categoryId(categoryId)
                .status(1)
                .build();
        return bookDishMapper.list(bookDishDTO);
    }
}
